// pages/index/index.js
import API from "../../api/index";
const app = getApp()
import {
  getDate,comptime,yesterday
} from '../../utils/common'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbarData:{
      showCapsule:1,//是否显示右上角图标 1表示显示 0表示不显示
      title:'',//同事名字
      logo:'',//同事头像
      userId:''//同事id
    },
    userId:'',//同事id
    compare:false,//判断大于等于今天就能添加，查看，checkbox
    cause:'',
    yesterday:'',//昨天
    // circular:true,
    height:'',
    today: "",
    explain: '今日',
    explainShow: true,
    current: 1,
    date: '',
    //工作任务
    jobId:-1,//任务id
    list: [
      {
        id: 0,
        time: '',
        jobList: [],
        studyList: [],
    },
     {
        id: 0,
        time: '',
        jobList: [],
        studyList: [],
    },
    {
      id: 0,
      time: '',
      jobList: [],
      studyList: [],
  },
  ],
    bindshowIndex: '',//滑动下标
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userid =  options.id
    let avatar =  options.avatar
    let name =  options.name
    if(userid!=''&&userid!=undefined){
      this.setData({userId:userid,['navbarData.userId']:userid,['navbarData.logo']:avatar,['navbarData.title']:name})
    }
    this.setData({
      height: app.globalData.height*2+20,
    })
    let date = getDate(2)
    this.setData({ date: date })
    this.setData({yesterday:yesterday()})
    //今天的时间
    var day2 = new Date();
    day2.setTime(day2.getTime());
    // var s2 = day2.getFullYear() + "-" + (day2.getMonth() + 1) + "-" + day2.getDate();
    var s2 = day2.getFullYear() + "-" + ((day2.getMonth() + 1).toString().length<2?('0'+(day2.getMonth() + 1)):day2.getMonth() + 1) + "-" +  (day2.getDate().toString().length<2?('0'+day2.getDate()):day2.getDate())
    this.setData({
      today: s2
    })
  },

  // 上一页
  previous: function () {
    // console.info('获取上一页,先看看今天是什么时候', this.data.today)
    var day1 = new Date(this.data.today);
    day1.setTime(day1.getTime() - 24 * 60 * 60 * 1000);
    var newToday = day1.getFullYear() + "-" + ((day1.getMonth() + 1).toString().length<2?('0'+(day1.getMonth() + 1)):day1.getMonth() + 1) + "-" +  (day1.getDate().toString().length<2?('0'+day1.getDate()):day1.getDate())
    
    // var newToday = day1.getFullYear() + "-" + (day1.getMonth() + 1) + "-" + day1.getDate();
    this.setData({
      today: newToday
    })
    let s1 = new Date()
    var startDate = Date.parse(newToday);
    var endDate = Date.parse(s1);
    var days = (endDate - startDate) / (1 * 24 * 60 * 60 * 1000);
    days = Math.floor(days)
    if (days == 1) {
      this.setData({
        explain: '昨日',
        explainShow: true
      })
    } else if (days == 0) {
      this.setData({
        explain: '今日',
        explainShow: true
      })
    } else if (days == -1) {
      this.setData({
        explain: '明日',
        explainShow: true
      })
    } else {
      this.setData({
        explain: '',
        explainShow: false
      })
    }
    this.setData({compare:comptime(this.data.date,this.data.today)})
    this.updateCause()
    this._getDayJob()
  },

  // 后一天
  next: function () {
    // console.info('获取后一天,先看看今天是什么时候', this.data.today)
    let current = this.data.current + 1
    for (var i = 0; i < this.data.list.length; i++) {
      if (current == this.data.list[i].id) {
        this.setData({
          today: this.data.list[i].time,
          current: current
        })
      }
    }
    var day3 = new Date(this.data.today);
    day3.setTime(day3.getTime() + 24 * 60 * 60 * 1000);
    // var newToday = day3.getFullYear() + "-" + (day3.getMonth() + 1) + "-" + day3.getDate();
    var newToday = day3.getFullYear() + "-" + ((day3.getMonth() + 1).toString().length<2?('0'+(day3.getMonth() + 1)):day3.getMonth() + 1) + "-" +  (day3.getDate().toString().length<2?('0'+day3.getDate()):day3.getDate())
    
    this.setData({
      today: newToday
    })
    let s1 = new Date()
    var startDate = Date.parse(s1);
    var endDate = Date.parse(newToday);
    var days = (endDate - startDate) / (1 * 24 * 60 * 60 * 1000);
    days = Math.ceil(days)
    if (days == 1) {
      this.setData({
        explain: '明日',
        explainShow: true
      })
    } else if (days == 0) {
      this.setData({
        explain: '今日',
        explainShow: true
      })
    } else if (days == -1) {
      this.setData({
        explain: '昨日',
        explainShow: true
      })
    } else {
      this.setData({
        explain: '',
        explainShow: false
      })
    }
    this.setData({compare:comptime(this.data.date,this.data.today)})
    this.updateCause()
    this._getDayJob()
  },
  updateCause(){
    API.getLastTime().then(res=>{
      if(res.data!=''){
        let v1 = res.data.replace('\/','-')
        let v2 = v1.replace('\/','-')
        this.setData({cause:v2})
      }else{
        this.setData({cause:''})
      }
    })
  },
  // 滑动页面改变日期
  swiperTab: function (e) {
    // for (var i = 0; i < this.data.list.length; i++) {
    //   if (e.detail.current == this.data.list[i].id) {
          if(e.detail.current==0){
            this.previous()
          }else if(e.detail.current==2){
              this.next()
          }

        this.setData({
          current: 1
        })
    
  },
  explain(){
    let s1 = new Date()
    var m = s1.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = s1.getDate();
    d = d < 10 ? ('0' + d) : d;
    s1 = s1.getFullYear() + '-' + m + '-' + d
    var startDate = Date.parse(s1);
    var endDate = Date.parse(this.data.today);
    var days = (startDate - endDate) / (1 * 24 * 60 * 60 * 1000);
    days = Math.round(days)
    // 
    // let s1 = new Date()
    // var startDate = Date.parse(s1);
    // var endDate = Date.parse(this.data.today);
    // var days = (endDate - startDate) / (1 * 24 * 60 * 60 * 1000);
    // days = Math.round(days)
    if (days == 1) {
      this.setData({
        explain: '明日',
        explainShow: true
      })
    } else if (days == 0) {
      this.setData({
        explain: '今日',
        explainShow: true
      })
    } else if (days == -1) {
      this.setData({
        explain: '昨日',
        explainShow: true
      })
    } else {
      this.setData({
        explain: '',
        explainShow: false
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this._getDayJob()
  },
  _getDayJob(){
    this.setData({
      ['list[1].jobList']:{},
      ['list[1].studyList']:{}
    })
    let obj = {
      time:this.data.today.replace(/-/g,'/')
    }
    if(this.data.userId!=''){
      obj.user_id=this.data.userId
    }
    API.getColleague(obj).then(res=>{
      this.setData({
        ['list[1].jobList']:res.data.work_list,
        ['list[1].studyList']:res.data.study_list
      })
      
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  nav(){
    this.setData({userId:'',['navbarData.userId']:'',['navbarData.logo']:'',['navbarData.title']:''})
    // wx.redirectTo({
    //   url: '/pages/lichuan_team/index',
    // })
    wx.navigateBack()
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})